package com.hxzy.controller;

import com.github.pagehelper.PageInfo;
import com.hxzy.pojo.Book;
import com.hxzy.pojo.BookType;
import com.hxzy.service.BookService;
import com.hxzy.service.BookTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

/**
 * @author zl
 * @create 2020-10-28 9:57
 */
@Controller
public class BookController {
    @Autowired
    private BookService bookService;

    @Autowired
    private BookTypeService bookTypeService;
    @RequestMapping(value = "/getAllBook",method = RequestMethod.GET)
    public String getAllBook(HttpServletRequest request){
        List<Book> books = bookService.selectAll();
        request.setAttribute("book",books);
//        return "manager/book_manager";
        return "redirect:/getBooks";
    }

    @RequestMapping(value = "/addBook",method = RequestMethod.POST)
    public String addBook(Book book){
        bookService.addBook(book);
        return "redirect:/getBooks?pageNum="+Integer.MAX_VALUE;
    }

    @RequestMapping(value = "/deletedBook",method = RequestMethod.DELETE)
    public String deletedBook(@RequestParam(value = "id") Integer id,@RequestHeader("referer")String referer) {
        int i = bookService.deleteBook(id);
        return "redirect:" + referer;
    }

    @RequestMapping(value = "/selectBookById",method =RequestMethod.GET)
    public String selectBookById(@RequestParam(value = "id")Integer id,HttpServletRequest request){
        Book book = bookService.selectBookById(id);
        List<BookType> type = bookTypeService.selectAll();
        request.setAttribute("book",book);
        request.setAttribute("type",type);
        return "manager/book_update";
    }

    @RequestMapping(value = "/updateBook",method = RequestMethod.PUT)
    public String updateBook(Book book){
        bookService.updateBook(book);
        return "redirect:/getBooks";
    }

    @RequestMapping(value = "/getBook",method = RequestMethod.GET)
    public ModelAndView getBook(@RequestParam(value = "pageNum",required = true,defaultValue = "1") Integer pageNum,@RequestParam(value = "pageSize",required = false,defaultValue = "4")Integer pageSize,HttpServletRequest request){
        PageInfo<Book> bookByPage = bookService.getBookByPage(pageNum, pageSize);
        List<Book> list = bookByPage.getList();
        ModelAndView mav = new ModelAndView();
        mav.addObject("book",list);
        mav.addObject("page",bookByPage);
        mav.addObject("url","getBook?");
        mav.setViewName("client/index");
        return mav;
    }

    @RequestMapping(value = "/getBooks",method = RequestMethod.GET)
    public ModelAndView getBooks(@RequestParam(value = "pageNum",required = true,defaultValue = "1") Integer pageNum,@RequestParam(value = "pageSize",required = false,defaultValue = "4")Integer pageSize,HttpServletRequest request){
        PageInfo<Book> bookByPage = bookService.getBookByPage(pageNum, pageSize);
        List<Book> list = bookByPage.getList();
        ModelAndView mav = new ModelAndView();
        mav.addObject("book",list);
        mav.addObject("page",bookByPage);
        mav.addObject("url","getBooks?");
        mav.setViewName("manager/book_manager");
        return mav;
    }

    @RequestMapping("/getBooksByPrice")
    public ModelAndView getBooksByPrice(@RequestParam(value = "pageNum",required = true,defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize",required = false,defaultValue = "4")Integer pageSize,
                                        @RequestParam(value = "min",required = false,defaultValue = "0") Double min,
                                        @RequestParam(value = "max",required = false,defaultValue = "1000")Double max){
        PageInfo<Book> booksByPrice = bookService.getBooksByPrice(pageNum, pageSize, min, max);
        List<Book> list = booksByPrice.getList();
        ModelAndView mav = new ModelAndView();
        mav.addObject("book",list);
        mav.addObject("min",min);
        mav.addObject("max",max);
        mav.addObject("page",booksByPrice);
        mav.addObject("url","getBooksByPrice?min="+min+"&max="+max+"&");
        mav.setViewName("client/index");
        return mav;
    }


    @RequestMapping("/uploadHeard")
    @ResponseBody
    public String uploadHeard(MultipartFile heardImg, HttpServletRequest request) throws IOException {
// 1.设置上传文件保存的位置
        ServletContext servletContext = request.getSession().getServletContext();
        String realPath = servletContext.getRealPath("/upload");
// 判断该文件夹是否存在,如果不存在,新建文件夹
        File directory = new File(realPath);
        if (!directory.exists()){
            directory.mkdirs();
        }

        String filename = heardImg.getOriginalFilename();

        filename = URLEncoder.encode(filename,"utf-8");
        filename = filename.replaceAll("%","-");
        filename = UUID.randomUUID() + "_" + filename;

        File file = new File(realPath,filename);

        // 将接收的数据写到空文件中
        heardImg.transferTo(file);
        return "/upload/"+filename;
    }
}
